package controllayer;

import static org.junit.Assert.*;
import modellayer.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * The Class OrderControllerTest.
 *@author Benjamin Ørskov
 *@author Lasse Lambertsen
 *@author Ole Kirk
 *@author Søren Søgaard
 */
public class OrderControllerTest {

	/** The order co1. */
	controllayer.OrderController orderCo1 = new controllayer.OrderController();

	/** The person co1. */
	controllayer.PersonController personCo1 = new controllayer.PersonController();

	/** The item co1. */
	controllayer.ItemController itemCo1 = new controllayer.ItemController();

	/** The salesman counter. */
	private int salesmanCounter = 0;

	/** The order counter. */
	private static int orderCounter = 1;

	/**
	 * Sets the up.
	 *
	 * @throws Exception
	 *             the exception
	 */
	@Before
	public void setUp() throws Exception {
		personCo1.createCustomer("Ole", "Vesterbro", "Aalborg",
				"olex999@live.dk", "123");
		personCo1.createSalesman("Benja", "Eternitten", "AA",
				"leckcy@gmail.com", "456", 1.0);
		itemCo1.createItem("Skrue", 1, 10.0, 1000);
		itemCo1.createItem("Skruetrækker", 2, 100.0, 100);
		salesmanCounter = personCo1.getSalesmanID();
	}

	/**
	 * Tear down.
	 *
	 * @throws Exception
	 *             the exception
	 */
	@After
	public void tearDown() throws Exception {
		Customer c = (Customer) personCo1.findCustomer("123");
		personCo1.removePerson(c);
		Salesman s = (Salesman) personCo1.findSalesman(salesmanCounter - 1);
		personCo1.removePerson(s);
		itemCo1.removeItem(1);
		itemCo1.removeItem(2);
		orderCo1.removeOrder(0);
		orderCounter++;
	}

	/**
	 * Test create order.
	 */
	@Test
	public void testCreateOrder() {
		Order o = orderCo1.createOrder();
		assertNotNull(o);
		Customer c = (Customer) personCo1.findCustomer("123");
		assertNotNull(c);
		o.setCustomer(c);
		Salesman s = (Salesman) personCo1.findSalesman(salesmanCounter - 1);
		assertNotNull(s);
		o.setSalesman(s);
		Item i = itemCo1.findItem(1);
		assertNotNull(i);
		orderCo1.addItem(i, 5, o);
		Item i2 = itemCo1.findItem(2);
		assertNotNull(i2);
		orderCo1.addItem(i2, 1, o);
	}

	/**
	 * Test find order.
	 */
	@Test
	public void testFindOrder() {
		Order o = orderCo1.createOrder();
		assertNotNull(o);
		Customer c = (Customer) personCo1.findCustomer("123");
		assertNotNull(c);
		o.setCustomer(c);
		Salesman s = (Salesman) personCo1.findSalesman(salesmanCounter - 1);
		assertNotNull(s);
		o.setSalesman(s);
		Item i = itemCo1.findItem(1);
		assertNotNull(i);
		orderCo1.addItem(i, 5, o);
		Item i2 = itemCo1.findItem(2);
		assertNotNull(i2);
		orderCo1.addItem(i2, 1, o);
		// Positive test
		assertNotNull(orderCo1.findOrder(orderCounter));
		// Negative test
		assertNull(orderCo1.findOrder(45478));
	}

	/**
	 * Test remove part order.
	 */
	@Test
	public void testRemovePartOrder() {
		Order o = orderCo1.createOrder();
		assertNotNull(o);
		Customer c = (Customer) personCo1.findCustomer("123");
		assertNotNull(c);
		o.setCustomer(c);
		Salesman s = (Salesman) personCo1.findSalesman(salesmanCounter - 1);
		assertNotNull(s);
		o.setSalesman(s);
		Item i = itemCo1.findItem(1);
		assertNotNull(i);
		orderCo1.addItem(i, 5, o);
		Item i2 = itemCo1.findItem(2);
		assertNotNull(i2);
		orderCo1.addItem(i2, 1, o);
		o = orderCo1.findOrder(orderCounter);
		assertNotNull(o);
		orderCo1.removePartOrder(o, "Skrue"); // This will succeed
		// Negative test
		try {
			orderCo1.removePartOrder(o, "Skrue");
		} catch (UpdateIncompleteException uie) {
			assertEquals("Vare ikke fjernet!", uie.getMessage());
		}
	}

	/**
	 * Test delete order.
	 */
	@Test
	public void testDeleteOrder() {
		Order o = orderCo1.createOrder();
		assertNotNull(o);
		Customer c = (Customer) personCo1.findCustomer("123");
		assertNotNull(c);
		o.setCustomer(c);
		Salesman s = (Salesman) personCo1.findSalesman(salesmanCounter - 1);
		assertNotNull(s);
		o.setSalesman(s);
		Item i = itemCo1.findItem(1);
		assertNotNull(i);
		orderCo1.addItem(i, 5, o);
		Item i2 = itemCo1.findItem(2);
		assertNotNull(i2);
		orderCo1.addItem(i2, 1, o);
		// Positive test
		o = orderCo1.findOrder(orderCounter);
		assertNotNull(o);
		orderCo1.removeOrder(orderCounter);
		// Negative test
		assertNull(orderCo1.findOrder(orderCounter));
		orderCo1.removeOrder(orderCounter);

	}
}